@@ -185,17 +185,36 @@ def preview_async(request):
185
185
186
186
187
187
def _add_recipients (msg ):
188
+ """Process and attach recipient information to a message object.
189
+
190
+ This helper function calculates recipient counts and assigns recipient-related
191
+ attributes to the message object for both individual users and groups.
192
+
193
+ Args:
194
+ msg: An OutboxMessage object to process.
195
+
196
+ Returns:
197
+ The modified message object with the following attributes set:
198
+ - recipients_count: Number of individual recipients
199
+ - to_groups_count: Number of group recipients
200
+ - recipient: The single recipient (if exactly one), else None
201
+ - recipient.is_bot_user: Boolean indicating if recipient is SumoBot
202
+ - to_groups: List of recipient groups with prefetched profiles
203
+
204
+ Note:
205
+ The function assumes msg.to and msg.to_group are valid related fields
206
+ on the message object.
207
+ """
188
208
# Set the counts based on the lists
189
209
msg .recipients_count = msg .to .all ().count ()
190
210
msg .to_groups_count = msg .to_group .all ().count ()
191
211
192
212
# Assign the recipient based on the number of recipients
193
213
msg .recipient = msg .to .all ()[0 ] if msg .recipients_count == 1 else None
194
214
195
- if msg .recipient .username == "SumoBot" :
196
- msg .recipient .is_bot_user = True
197
- else :
198
- msg .recipient .is_bot_user = False
215
+ # Set is_bot_user flag only if there's a recipient with a username
216
+ if msg .recipient and hasattr (msg .recipient , "username" ):
217
+ msg .recipient .is_bot_user = msg .recipient .username == "SumoBot"
199
218
200
219
# Assign the group(s) based on the number of groups
201
220
msg .to_groups = list (msg .to_group .prefetch_related ("profile" ))
0 commit comments